package com.cysion.study.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.other.color
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.widget.ShowToast
import kotlinx.android.synthetic.main.activity_addback.*

class AddBankActivity : BaseActivity(), View.OnClickListener {


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 301) {
            if (resultCode == Activity.RESULT_OK) {
                var name = data?.getBundleExtra("key")?.getString("name")
                tv_bank.text = name
            }
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_addback

    override fun initView() {
        whiteTextTheme(color(R.color.main_cl))
        topbar.apply {
            setTitle("添加银行卡")
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }
            }
        }
        tvNext.setOnClickListener(this)
        tv_bank.setOnClickListener(this)
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tvNext -> {
                var name = et_name.textEx.trim()
                var num = et_num.textEx.trim()
                var bank = et_bank.textEx.trim()
                var address = et_address.textEx.trim()
                var card = et_card.textEx.trim()
                var phone = et_phone.textEx.trim()
                /*if (TextUtils.isEmpty(bank)) {
                    ShowToast("请输入开户银行")
                    return
                }*/
                et_name.checkBlank("姓名不能为空") ?: return
                et_num.checkBlank("身份证号不能为空") ?: return
                et_bank.checkBlank("请输入开户银行") ?: return
                et_address.checkBlank("请输入开户行地址") ?: return
                et_card.checkBlank("请输入银行卡号") ?: return
                et_phone.checkBlank("请输入预留手机号") ?: return

                Alert.loading(self)
                ApiClient.api.addbank(UserCache.token, name, num, bank, address, card, phone)
                    .compose(BaseResponseRx.validateToMain())
                    ._subscribe {
                        _onNext {
                            Alert.close()
                            ShowToast("添加成功")
                            setResult(Activity.RESULT_OK)
                            finish()
                        }
                        _onError {
                            Alert.close()
                            ShowToast(ErrorHandler.handle(it).errorMsg)
                        }
                    }


            }
            R.id.tv_bank -> {
//                startActivityForResult_ex<BankListActivity>(301)//选择银行
            }
        }
    }

    override fun closeMvp() {
    }

}
